/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.reader;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.feiyue.BookCatologActivity;
import com.feiyue.BookCommentActivity;
import com.feiyue.R;
import com.feiyue.basic.reader.dao.FileRecordDao;
import com.feiyue.basic.reader.dao.SettingDao;
import com.feiyue.basic.reader.dialog.LoadingDialog;
import com.feiyue.basic.reader.dialog.PageSettingAboutDialog;
import com.feiyue.basic.reader.dialog.PageSettingBgDialog;
import com.feiyue.basic.reader.pojo.FileRecord;
import com.feiyue.basic.reader.pojo.Setting;
import com.feiyue.basic.reader.test.BookPageFactory;
import com.feiyue.basic.reader.test.PageWidget;
import com.feiyue.basic.reader.util.IsNetworkUtil;
import com.feiyue.basic.reader.util.LightUtil;
import com.feiyue.basic.reader.util.LogUtil;
import com.feiyue.basic.reader.util.PhotoUtil;
import com.feiyue.basic.reader.view.FontLightTabMenu;
import com.feiyue.basic.reader.view.PlanTabMenu;
import com.feiyue.basic.server.cache.CacheFetch;
import com.feiyue.basic.server.cache.DiskLruCache;
import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Chapter;
import com.genius.demo.Menu;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PageActivity extends Activity
{
    public class CatologUpdateMessageReceiver extends BroadcastReceiver
    {

        public void onReceive(Context context1, Intent intent)
        {
            String s = intent.getStringExtra("BookID");
            if (s.equals((new StringBuilder(String.valueOf(bookid))).toString()))
            {
                chapterlist = null;
            } else
            {
                if (s.equals("网络不畅通"))
                    pageActivity.finish();
                Toast.makeText(context1, s, 0).show();
            }
        }
    }

    public class PageSettingBgReceiver extends BroadcastReceiver
    {

        public void onReceive(Context context1, Intent intent)
        {
            Integer integer;
            integer = Integer.valueOf(intent.getIntExtra("position", 0));
            if (nWidth > 1.0F || nHeight > 1.0F)
                PageActivity.mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), pages[integer.intValue()]), nWidth, nHeight);
            else
                PageActivity.mBgBitmap = BitmapFactory.decodeResource(mContext.getResources(), pages[integer.intValue()]);
            if (integer.intValue() != 0) {
            	if (integer.intValue() == 1)
                    pagefactory.setTextColor(getResources().getColor(0x7f09000c));
                else
                if (integer.intValue() == 2)
                    pagefactory.setTextColor(getResources().getColor(0x7f09000d));
                else
                if (integer.intValue() == 3)
                    pagefactory.setTextColor(getResources().getColor(0x7f09000e));
            } else {
            	pagefactory.setTextColor(getResources().getColor(0x7f09000b));
            }
            pagefactory.setBgBitmap(PageActivity.mBgBitmap);
            PageActivity.mBgBitmap = null;
            pagefactory.onDraw(mNextPageCanvas);
            mPageWidget.setBitmaps(PageActivity.mNextPageBitmap, PageActivity.mNextPageBitmap);
            mPageWidget.postInvalidate();
            setting.setBg(integer.intValue());
            setting.setDayType(0);
        }
    }

    public class PageSettingReceiver extends BroadcastReceiver
    {

        public void onReceive(Context context1, Intent intent)
        {
            int position = Integer.valueOf(intent.getIntExtra("position", 0)).intValue();
            
            switch (position) {
			case 0:
				fontLightTabMenu = new FontLightTabMenu(mContext, new TitleClickEvent(), 0, 0x7f0a0003, -10 + setting.getFontSize(), (int)(10F * setting.getLight()));
	            if (fontLightTabMenu != null)
	                if (fontLightTabMenu.isShowing())
	                    fontLightTabMenu.dismiss();
	                else
	                    fontLightTabMenu.showAtLocation(mPageWidget, 80, 0, 0);
				break;
			case 1:
				pageSettingBgDialog = new PageSettingBgDialog(mContext, 0x7f0a0009);
	            pageSettingBgDialog.show();
				break;
			case 2:
				if (setting.getDayType() != 1) {
					PageActivity.mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), 0x7f020060), nWidth, nHeight);
		            setting.setDayType(1);
		            pagefactory.setTextColor(getResources().getColor(0x7f09000f));
				} else {
					PageActivity.mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), pages[setting.getBg()]), nWidth, nHeight);
		            setting.setDayType(0);
		            if (setting.getBg() != 0) {
		            	if (setting.getBg() == 1)
		                    pagefactory.setTextColor(getResources().getColor(0x7f09000c));
		                else
		                if (setting.getBg() == 2)
		                    pagefactory.setTextColor(getResources().getColor(0x7f09000d));
		                else
		                if (setting.getBg() == 3)
		                    pagefactory.setTextColor(getResources().getColor(0x7f09000e));
		            } else {
		            	pagefactory.setTextColor(getResources().getColor(0x7f09000b));
		            }
				}
				
				pagefactory.setBgBitmap(PageActivity.mBgBitmap);
                PageActivity.mBgBitmap = null;
                pagefactory.onDraw(mNextPageCanvas);
                mPageWidget.setBitmaps(PageActivity.mNextPageBitmap, PageActivity.mNextPageBitmap);
                mPageWidget.postInvalidate();
                
				break;
			case 3:
				int i = 0;
	            startNum = pagefactory.getBufEnd();
	            if (startNum > 0)
	                i = (int)(100F * ((1.0F * (float)startNum) / (float)totalNum));
	            planTabMenu = new PlanTabMenu(mContext, new TitleClickEvent(), 0, 0x7f0a0003, i);
	            if (planTabMenu != null)
	                if (planTabMenu.isShowing())
	                    planTabMenu.dismiss();
	                else
	                    planTabMenu.showAtLocation(mPageWidget, 80, 0, 0);
				break;
			case 4:
				pageSettingAboutDialog = new PageSettingAboutDialog(mContext, 0x7f0a0009);
	            pageSettingAboutDialog.show();
				break;

			default:
				break;
			}
        }
    }

    public class TabMenuReceiver extends BroadcastReceiver
    {

        public void onReceive(Context context1, Intent intent)
        {
            String s;
            int i;
            s = intent.getStringExtra("type");
            i = intent.getIntExtra("position", 1);
            if (!s.equalsIgnoreCase("font")) {
            	if (s.equalsIgnoreCase("light"))
                {
                    float f = i;
                    lightFloat = f / 20F;
                    if (lightFloat == 0.0F)
                        lightFloat = 0.1F;
                    LightUtil.stopAutoBrightness((Activity)mContext);
                    LightUtil.setLight(lightFloat, (Activity)mContext);
                    setting.setLight(lightFloat);
                } else
                if (s.equalsIgnoreCase("plan"))
                {
                    Log.i("dd", (new StringBuilder(String.valueOf(i * (pagefactory.getTotalNum() / 100)))).toString());
                    pagefactory.setPlan(i * (pagefactory.getTotalNum() / 100));
                    if (ispage)
                        pagefactory.onDraw(mNextPageCanvas);
                    else
                        pagefactory.onDraw(mCurPageCanvas);
                    mPageWidget.setBitmaps(PageActivity.mCurPageBitmap, PageActivity.mNextPageBitmap);
                    mPageWidget.postInvalidate();
                }
            } else {
            	 pagefactory.setFontSize(i + 10);
                 pagefactory.onDraw(mNextPageCanvas);
                 mPageWidget.setBitmaps(PageActivity.mNextPageBitmap, PageActivity.mNextPageBitmap);
                 mPageWidget.postInvalidate();
                 setting.setFontSize(i + 10);
            }
        }
    }

    class TitleClickEvent
        implements android.view.View.OnClickListener
    {

        public void onClick(View view)
        {
        }
    }


    public static final String NetError = "网络不畅通";
    public static final String RECEIVER = "tab_menu";
    public static Bitmap mBgBitmap;
    public static Bitmap mCurPageBitmap;
    public static Bitmap mNextPageBitmap;
    private String author;
    private int bookid;
    private String bookname;
    private int categoryid;
    private String categoryname;
    private int chapterNum;
    private int chapterid;
    private List chapterlist;
    private Context context;
    private DisplayMetrics displayMetrics;
    private String filePath;
    private FileRecordDao fileRecordDao;
    private FontLightTabMenu fontLightTabMenu;
    protected Handler handler;
    private int height;
    protected boolean initialstate;
    private String introduction;
    private boolean isNext;
    private boolean ispage;
    private String issuestate;
    private float lightFloat;
    private List list;
    private LoadingDialog loadingDialog;
    private Context mContext;
    private Canvas mCurPageCanvas;
    Handler mHandler;
    private Canvas mNextPageCanvas;
    public PageWidget mPageWidget;
    private Menu menu;
    private CatologUpdateMessageReceiver messageReceiver;
    private float nHeight;
    private float nWidth;
    private PageActivity pageActivity;
    private PageSettingAboutDialog pageSettingAboutDialog;
    private PageSettingBgDialog pageSettingBgDialog;
    private PageSettingBgReceiver pageSettingBgreceiver;
    private BookPageFactory pagefactory;
    private int pages[];
    private android.widget.LinearLayout.LayoutParams params2;
    private PlanTabMenu planTabMenu;
    private LinearLayout readLinearLayout;
    private Setting setting;
    private SettingDao settingDao;
    private int startNum;
    private TabMenuReceiver tabMenuReceiver;
    private String tag;
    private int totalNum;
    private int width;

    public PageActivity()
    {
        pageActivity = this;
        tag = PageActivity.class.getName();
        setting = new Setting();
        filePath = "";
        list = new ArrayList();
        totalNum = 0;
        startNum = 0;
        lightFloat = 2.0F;
        isNext = true;
        width = 480;
        height = 800;
        nWidth = 1.0F;
        nHeight = 1.0F;
        ispage = false;
        int ai[] = new int[4];
        ai[0] = 0x7f02005c;
        ai[1] = 0x7f02005d;
        ai[2] = 0x7f02005e;
        ai[3] = 0x7f02005f;
        pages = ai;
        chapterlist = null;
        chapterNum = 0;
        initialstate = true;
        mHandler = new Handler() {

            public void handleMessage(Message message)
            {
            	switch (message.what) {
				case -1:
					Toast.makeText(mContext, "经过努力的加载还是失败，请检查网络", 0).show();
	                if (initialstate)
	                    pageActivity.finish();
					break;
				case 0:
					PageActivity pageactivity = PageActivity.this;
	                pageactivity.chapterNum = -1 + pageactivity.chapterNum;
	                Toast.makeText(mContext, "暂时没有章节", 0).show();
	                if (initialstate)
	                    pageActivity.finish();
					break;
				case 1:
					Log.d(PageActivity.class.getName(), (new StringBuilder("path:")).append(filePath).toString());
	                try {
						pagefactory.openbook(filePath);
						
						if (!isNext) {
		                	pagefactory.setPlan(pagefactory.getLastPosition());
		                } else {
		                	pagefactory.setPlan(0);
		                }
		                
		                if (startNum > 0)
	                        pagefactory.setPlan(startNum);
	                    pagefactory.setChapterName(((Chapter)chapterlist.get(chapterNum)).getName());
	                    pagefactory.onDraw(mCurPageCanvas);
					} catch (IOException e) {
						Toast.makeText(mContext, 0x7f08004a, 0).show();
						e.printStackTrace();
					} finally {
						mPageWidget.setBitmaps(PageActivity.mCurPageBitmap, PageActivity.mCurPageBitmap);
	                    mPageWidget.postInvalidate();
					}
                    
					break;
				default:
					break;
				}
            	
            	if (loadingDialog != null)
                    loadingDialog.cancel();
            }
        };
    }

    private boolean fileExist()
    {
        boolean flag;
        if (chapterNum >= chapterlist.size())
        {
            flag = true;
        } else
        {
            int i = chapterNum;
            int j;
            String s;
            File file;
            DiskLruCache disklrucache;
            if (chapterNum == -1)
                j = ((Chapter)chapterlist.get(0)).getId();
            else
                j = ((Chapter)chapterlist.get(i)).getId();
            s = (new StringBuilder(String.valueOf(bookid))).append("_").append(j).toString();
            file = DiskLruCache.getDiskCacheDir(context, "http");
            disklrucache = DiskLruCache.openCache(context, file, 0xa00000L);
            if (disklrucache == null)
                flag = false;
            else
            if (disklrucache.containsKey(s))
                flag = true;
            else
                flag = false;
        }
        return flag;
    }

    private void getmenu()
    {
        menu = new Menu(this, 
        		R.array.menu_item_name_array_readermenufirstlevel, R.drawable.menu_image_list1,
        		R.array.menu_item_name_array2, R.drawable.menu_image_list1,
        		R.layout.menu, R.id.menuGridChange, R.layout.menu_item,
        		R.id.imageview, R.id.textview, R.id.pagescreen, new android.widget.AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView adapterview, View view, int i, long l)
            {
                int j = i + 1;
                if (j != 8) {
                	if (j == 1 && menu.mIsMore)
                    {
                        Intent intent1 = new Intent(getApplicationContext(), BookCatologActivity.class);
                        intent1.putExtra("categoryname", categoryname);
                        intent1.putExtra("categoryid", categoryid);
                        intent1.putExtra("bookname", bookname);
                        intent1.putExtra("bookid", bookid);
                        intent1.putExtra("author", author);
                        intent1.putExtra("introduction", introduction);
                        intent1.putExtra("issuestate", issuestate);
                        intent1.setFlags(0x20000);
                        startActivity(intent1);
                    } else if (j == 2 && menu.mIsMore)
                    {
                        menu.hideMenu();
                        fontLightTabMenu = new FontLightTabMenu(mContext, new TitleClickEvent(), 0, 0x7f0a0003, -10 + setting.getFontSize(), (int)(10F * setting.getLight()));
                        if (fontLightTabMenu != null)
                            if (fontLightTabMenu.isShowing())
                                fontLightTabMenu.dismiss();
                            else
                                fontLightTabMenu.showAtLocation(mPageWidget, 80, 0, 0);
                    } else if (j == 3 && menu.mIsMore)
                    {
                        menu.hideMenu();
                        pageSettingBgDialog = new PageSettingBgDialog(mContext, 0x7f0a0009);
                        pageSettingBgDialog.show();
                    } else if (j != 4 || !menu.mIsMore) {
                    	if (j == 5 && menu.mIsMore)
                        {
                            menu.hideMenu();
                            int k = 0;
                            startNum = pagefactory.getBufEnd();
                            if (startNum > 0)
                                k = (int)(100F * ((1.0F * (float)startNum) / (float)totalNum));
                            planTabMenu = new PlanTabMenu(mContext, new TitleClickEvent(), 0, 0x7f0a0003, k);
                            if (planTabMenu != null)
                                if (planTabMenu.isShowing())
                                    planTabMenu.dismiss();
                                else
                                    planTabMenu.showAtLocation(mPageWidget, 80, 0, 0);
                        } else
                        if (j == 6 && menu.mIsMore)
                        {
                            menu.hideMenu();
                            Intent intent = new Intent(context, BookCommentActivity.class);
                            intent.putExtra("bookid", bookid);
                            startActivity(intent);
                        } else
                        if (j == 7 && menu.mIsMore)
                            pageActivity.finish();
                        else
                        if ((j != 1 || menu.mIsMore) && (j != 2 || menu.mIsMore) && (j != 3 || menu.mIsMore) && (j != 4 || menu.mIsMore) && (j != 5 || menu.mIsMore) && (j != 6 || menu.mIsMore) && j == 7)
                        {
                        	//TODO ?
//                            menu.mIsMore;
                        }
                    } else {
                    	menu.hideMenu();
                        if (setting.getDayType() != 1) {
                        	if (nWidth > 1.0F || nHeight > 1.0F)
                                PageActivity.mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), 0x7f020060), nWidth, nHeight);
                            else
                                PageActivity.mBgBitmap = BitmapFactory.decodeResource(mContext.getResources(), 0x7f020060);
                            setting.setDayType(1);
                            pagefactory.setTextColor(getResources().getColor(0x7f09000f));
                        } else {
                        	if (nWidth > 1.0F || nHeight > 1.0F)
                                PageActivity.mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), pages[setting.getBg()]), nWidth, nHeight);
                            else
                                PageActivity.mBgBitmap = BitmapFactory.decodeResource(mContext.getResources(), pages[setting.getBg()]);
                            setting.setDayType(0);
                            if (setting.getBg() != 0) {
                            	if (setting.getBg() == 1)
                                    pagefactory.setTextColor(getResources().getColor(0x7f09000c));
                                else
                                if (setting.getBg() == 2)
                                    pagefactory.setTextColor(getResources().getColor(0x7f09000d));
                                else
                                if (setting.getBg() == 3)
                                    pagefactory.setTextColor(getResources().getColor(0x7f09000e));
                            } else {
                            	pagefactory.setTextColor(getResources().getColor(0x7f09000b));
                            }
                            
                        }
                        pagefactory.setBgBitmap(PageActivity.mBgBitmap);
    	                PageActivity.mBgBitmap = null;
    	                pagefactory.onDraw(mNextPageCanvas);
    	                mPageWidget.setBitmaps(PageActivity.mNextPageBitmap, PageActivity.mNextPageBitmap);
    	                mPageWidget.postInvalidate();
                    }
                } else {
                	Toast.makeText(context, "暂无内容", 0).show();
                }
            }
        });
    }

    private void initiate(Intent intent)
    {
        totalNum = 0;
        startNum = 0;
        lightFloat = 2.0F;
        isNext = true;
        width = 480;
        height = 800;
        nWidth = 1.0F;
        nHeight = 1.0F;
        ispage = false;
        chapterNum = 0;
        initialstate = true;
        context = getApplicationContext();
        categoryname = intent.getExtras().getString("categoryname");
        categoryid = intent.getExtras().getInt("categoryid");
        bookname = intent.getExtras().getString("bookname");
        bookid = intent.getExtras().getInt("bookid");
        author = intent.getExtras().getString("author");
        introduction = intent.getExtras().getString("introduction");
        issuestate = intent.getExtras().getString("issuestate");
        chapterid = intent.getExtras().getInt("chapterid");
        chapterNum = intent.getExtras().getInt("chapterNum");
        mContext = this;
        if (fileRecordDao == null)
            fileRecordDao = new FileRecordDao(this);
        if (settingDao == null)
            settingDao = new SettingDao(this);
        list = fileRecordDao.getList(bookid);
        if (chapterNum == -1)
            if (list.size() > 0)
            {
                totalNum = ((FileRecord)list.get(0)).getNum();
                startNum = ((FileRecord)list.get(0)).getStartNum();
                chapterNum = ((FileRecord)list.get(0)).getChapterNum();
            } else
            {
                chapterNum = 0;
            }
        load();
        setting = (Setting)settingDao.getList().get(0);
        displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        width = displayMetrics.widthPixels;
        height = displayMetrics.heightPixels;
        nWidth = (float)((1.0D * (double)width) / 480D);
        nHeight = (float)((1.0D * (double)height) / 800D);
        mPageWidget = new PageWidget(this, width, height);
        readLinearLayout = (LinearLayout)findViewById(0x7f0600d4);
        readLinearLayout.removeAllViews();
        params2 = new android.widget.LinearLayout.LayoutParams(-1, height);
        readLinearLayout.setLayoutParams(params2);
        readLinearLayout.addView(mPageWidget);
        mCurPageBitmap = Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888);
        mNextPageBitmap = Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888);
        mCurPageCanvas = new Canvas(mCurPageBitmap);
        mNextPageCanvas = new Canvas(mNextPageBitmap);
        pagefactory = new BookPageFactory(width, height, setting.getFontSize(), startNum, getApplicationContext());
        setBgBitmap();
        pagefactory.setBgBitmap(mBgBitmap);
        mBgBitmap = null;
        try
        {
            pagefactory.openbook(filePath);
            pagefactory.onDraw(mCurPageCanvas);
        }
        catch (IOException ioexception) { }
        mPageWidget.setBitmaps(mCurPageBitmap, mCurPageBitmap);
        mPageWidget.setOnTouchListener(new android.view.View.OnTouchListener() {

            public boolean onTouch(View view, MotionEvent motionevent)
            {
                boolean flag = false;
                if (view == mPageWidget && !mPageWidget.flag) {
                    if (motionevent.getAction() == MotionEvent.ACTION_DOWN) {
                        if (motionevent.getX() > (float)((2 * width) / 5) && motionevent.getX() < (float)((3 * width) / 5) && motionevent.getY() > (float)((2 * height) / 5) && motionevent.getY() < (float)((3 * height) / 5))
                            menu.showMenuWindow();
                        else if (menu.mMenuView.isShown()) {
                            menu.showMenuWindow();
                        } else {
                            mPageWidget.abortAnimation();
                            mPageWidget.calcCornerXY(motionevent.getX(), motionevent.getY());
                            pagefactory.onDraw(mCurPageCanvas);
                            
                            if (mPageWidget.DragToRight()) {
                                try {
                                    pagefactory.prePage();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                
                                if (pagefactory.isfirstPage()) {
                                    pagefactory.setPlan(0);
                                    chapterNum = -1 + chapterNum;
                                    if (chapterNum >= 0) {
                                        isNext = false;
                                      startNum = 0;
                                      load();
                                    } else {
                                        chapterNum = 0;
                                        Toast.makeText(mContext, "第一页了！", 0).show();
                                        return false;
                                    }
                                }
                                
                                pagefactory.onDraw(mNextPageCanvas);
                            } else {
                                try {
                                    pagefactory.nextPage();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                
                                if(pagefactory.islastPage()) {
                                    chapterNum = 1 + chapterNum;
                                    isNext = true;
                                    startNum = 0;
                                    load();
                                }
                            }
                            
                            mPageWidget.setBitmaps(PageActivity.mCurPageBitmap, PageActivity.mNextPageBitmap);
                            
                            flag = mPageWidget.doTouchEvent(motionevent);
                            ispage = true;
                        }
                    } else {
                        flag = mPageWidget.doTouchEvent(motionevent);
                        ispage = true;
                    }
                    
                
                }
                return flag;
            }
        });
        LightUtil.setLight(setting.getLight(), this);
    }

    private void load()
    {
        if (chapterlist == null || chapterlist.size() == 0 || !fileExist())
            showLoadDialog();
        (new Thread(new Runnable() {

            public void run()
            {
                Message message;
                message = new Message();
                if (chapterlist == null)
                    chapterlist = NovelInfo.getChapterList(context, NovelInfo.getUrl2Chapterlist(bookid), categoryid, bookid, "cache first", handler);
                if (chapterlist != null && chapterlist.size() != 0) {
                	if (chapterNum >= chapterlist.size())
                    {
                        chapterNum = -1 + chapterlist.size();
                        if (loadingDialog != null && loadingDialog.isShowing())
                            loadingDialog.dismiss();
                        Intent intent1 = new Intent("CatologUpdate");
                        intent1.putExtra("BookID", "已是最后一章的最后一页");
                        sendBroadcast(intent1);
                    } else
                    {
                        String s;
                        Object aobj[];
                        String s1;
                        File file;
                        if (chapterNum == -1)
                        {
                            chapterid = ((Chapter)chapterlist.get(0)).getId();
                            chapterNum = 0;
                        } else
                        {
                            chapterid = ((Chapter)chapterlist.get(chapterNum)).getId();
                        }
                        s = (new StringBuilder(String.valueOf(bookid))).append("_").append(chapterid).toString();
                        aobj = new Object[2];
                        aobj[0] = Integer.valueOf(bookid);
                        aobj[1] = Integer.valueOf(chapterid);
                        s1 = String.format("http://book.yy567.net/bookContent?type=content&bookid=%d&contentid=%d", aobj);
                        LogUtil.d(tag, s1);
                        file = CacheFetch.getFile(context, s1, s, true);
                        if (file != null)
                        {
                            filePath = file.getPath();
                            message.what = 1;
                            initialstate = false;
                        } else
                        if (IsNetworkUtil.networkType(mContext) == -1)
                            message.what = -1;
                        else
                            message.what = 0;
                        mHandler.sendMessage(message);
                        if (file != null)
                            accelerate();
                    }
                } else {
                	Intent intent = new Intent("CatologUpdate");
                    intent.putExtra("BookID", "网络不畅通");
                    sendBroadcast(intent);
                }
            }
        })).start();
    }

    public static int px2dip(Context context1, float f)
    {
        return (int)(0.5F + f / context1.getResources().getDisplayMetrics().density);
    }

    private void setBgBitmap()
    {
        if (setting.getDayType() != 1) {
        	if (nWidth > 1.0F || nHeight > 1.0F)
                mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), pages[setting.getBg()]), nWidth, nHeight);
            else
                mBgBitmap = BitmapFactory.decodeResource(mContext.getResources(), pages[setting.getBg()]);
            if (setting.getBg() == 0)
                pagefactory.setTextColor(getResources().getColor(0x7f09000b));
            else
            if (setting.getBg() == 1)
                pagefactory.setTextColor(getResources().getColor(0x7f09000c));
            else
            if (setting.getBg() == 2)
                pagefactory.setTextColor(getResources().getColor(0x7f09000d));
            else
            if (setting.getBg() == 3)
                pagefactory.setTextColor(getResources().getColor(0x7f09000e));
        } else {
        	if (nWidth > 1.0F || nHeight > 1.0F)
                mBgBitmap = PhotoUtil.big(BitmapFactory.decodeResource(mContext.getResources(), 0x7f020060), nWidth, nHeight);
            else
                mBgBitmap = BitmapFactory.decodeResource(mContext.getResources(), 0x7f020060);
            pagefactory.setTextColor(getResources().getColor(0x7f09000f));
        }
    }

    private void showLoadDialog()
    {
        loadingDialog = new LoadingDialog(mContext, 0x7f0a000a);
        loadingDialog.show();
    }

    protected void accelerate()
    {
        if (chapterNum < -1 + chapterlist.size())
        {
            int j = ((Chapter)chapterlist.get(1 + chapterNum)).getId();
            String s2 = (new StringBuilder(String.valueOf(bookid))).append("_").append(j).toString();
            Object aobj1[] = new Object[2];
            aobj1[0] = Integer.valueOf(bookid);
            aobj1[1] = Integer.valueOf(j);
            String s3 = String.format("http://book.yy567.net/bookContent?type=content&bookid=%d&contentid=%d", aobj1);
            CacheFetch.getFile(context, s3, s2, true);
        }
        if (chapterNum > 0)
        {
            int i = ((Chapter)chapterlist.get(-1 + chapterNum)).getId();
            String s = (new StringBuilder(String.valueOf(bookid))).append("_").append(i).toString();
            Object aobj[] = new Object[2];
            aobj[0] = Integer.valueOf(bookid);
            aobj[1] = Integer.valueOf(i);
            String s1 = String.format("http://book.yy567.net/bookContent?type=content&bookid=%d&contentid=%d", aobj);
            CacheFetch.getFile(context, s1, s, true);
        }
    }

    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setTheme(0x103000e);
        setContentView(R.layout.page);
        handler = new Handler();
        messageReceiver = new CatologUpdateMessageReceiver();
        IntentFilter intentfilter = new IntentFilter("CatologUpdate");
        registerReceiver(messageReceiver, intentfilter);
        pageSettingBgreceiver = new PageSettingBgReceiver();
        registerReceiver(pageSettingBgreceiver, new IntentFilter("com.worlds.reader.dialog.PageSettingBgDialog"));
        tabMenuReceiver = new TabMenuReceiver();
        IntentFilter intentfilter1 = new IntentFilter("tab_menu");
        registerReceiver(tabMenuReceiver, intentfilter1);
        getmenu();
        initiate(getIntent());
    }

    public boolean onCreateOptionsMenu(android.view.Menu menu1)
    {
        menu1.add("menu");
        return super.onCreateOptionsMenu(menu1);
    }

    protected void onDestroy()
    {
        if (mBgBitmap != null && !mBgBitmap.isRecycled())
            mBgBitmap.recycle();
        mCurPageBitmap.recycle();
        mNextPageBitmap.recycle();
        FileRecord filerecord = new FileRecord();
        if (list.size() > 0)
        {
            filerecord = (FileRecord)list.get(0);
            filerecord.setNum(pagefactory.getTotalNum());
            filerecord.setStartNum(pagefactory.getBufEnd());
            filerecord.setBookId(bookid);
            filerecord.setCategoryId(categoryid);
            filerecord.setChapterId(chapterid);
            filerecord.setChapterNum(chapterNum);
        } else
        {
            filerecord.setPath(filePath);
            filerecord.setNum(pagefactory.getTotalNum());
            filerecord.setStartNum(pagefactory.getBufEnd());
            filerecord.setBookId(bookid);
            filerecord.setCategoryId(categoryid);
            filerecord.setChapterId(chapterid);
            filerecord.setChapterNum(chapterNum);
        }
        settingDao.save(setting);
        fileRecordDao.save(filerecord);
        fileRecordDao.close();
        settingDao.close();
        unregisterReceiver(tabMenuReceiver);
        unregisterReceiver(pageSettingBgreceiver);
        unregisterReceiver(messageReceiver);
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
        super.onDestroy();
    }

    public boolean onKeyDown(int i, KeyEvent keyevent)
    {
        return super.onKeyDown(i, keyevent);
    }

    public boolean onMenuOpened(int i, android.view.Menu menu1)
    {
        menu.showMenuWindow();
        return false;
    }

    protected void onNewIntent(Intent intent)
    {
        if (mBgBitmap != null && !mBgBitmap.isRecycled())
            mBgBitmap.recycle();
        mCurPageBitmap.recycle();
        mNextPageBitmap.recycle();
        initiate(intent);
        super.onNewIntent(intent);
    }

    protected void onPause()
    {
        FileRecord filerecord = new FileRecord();
        if (list.size() > 0)
        {
            filerecord = (FileRecord)list.get(0);
            filerecord.setNum(pagefactory.getTotalNum());
            filerecord.setStartNum(pagefactory.getBufEnd());
            filerecord.setBookId(bookid);
            filerecord.setCategoryId(categoryid);
            filerecord.setChapterId(chapterid);
            filerecord.setChapterNum(chapterNum);
        } else
        {
            filerecord.setPath(filePath);
            filerecord.setNum(pagefactory.getTotalNum());
            filerecord.setStartNum(pagefactory.getBufEnd());
            filerecord.setBookId(bookid);
            filerecord.setCategoryId(categoryid);
            filerecord.setChapterId(chapterid);
            filerecord.setChapterNum(chapterNum);
        }
        settingDao.save(setting);
        fileRecordDao.save(filerecord);
        super.onPause();
        MobclickAgent.onPause(this);
    }

    protected void onResume()
    {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    protected void onStop()
    {
        if (menu.mPopupWindow.isShowing())
            menu.mPopupWindow.dismiss();
        super.onStop();
    }

}